#---
# Excerpted from "Everyday Scripting in Ruby"
# We make no guarantees that this code is fit for any purpose. 
# Visit http://www.pragmaticprogrammer.com/titles/bmsft for more book information.
#---
require 'test/unit' 
require './churn'


class ChurnTests < Test::Unit::TestCase 

  def test_month_before_is_28_days
    assert_equal(Time.local(2005, 1, 1),
                 month_before(Time.local(2005, 1, 29)))
  end

  def test_header_format
    head = header(svn_date(month_before(Time.local(2011, 01, 25))),
                  svn_date(Time.local(2011, 12, 28)))
    assert_equal("Changes between 2010-12-28 and 2011-12-28:", head)
  end

  def test_image_for_divides_by_five
    assert_equal("****", image_for(20))
  end

  def test_image_for_rounds_up_and_down
    assert_equal('****', image_for(18))
    assert_equal('***', image_for(17))
  end

  def test_image_for_zero_is_a_dash
    assert_equal('-', image_for(0))
  end

  def test_image_for_rounds_up_small_number
    assert_equal('*', image_for(1))
    assert_equal('*', image_for(2))
    assert_equal('*', image_for(5))
    assert_equal('*', image_for(7))
    assert_equal('**', image_for(8))
  end

  def test_normal_subsystem_line_format
    assert_equal('audit          (45 changes)   *********',
                 subsystem_line("audit", 45))
  end

  def test_subsystem_line_has_special_format_for_zero_changes
    assert_equal('data           -              -',
                 subsystem_line("data", 0))
  end

  def test_svn_date
    assert_equal("2005-03-04",svn_date(Time.local(2005,3,4)))
  end

  def test_subversion_log_can_have_no_changes
    assert_equal(0, extract_change_count_from("------------------------------------------------------------------------\n"))
  end

  def test_subversion_log_with_changes
    assert_equal(2, extract_change_count_from("------------------------------------------------------------------------\nr2531 | bem | 2005-07-01 01:11:44 -0500 (Fri, 01 Jul 2005) | 1 line\n\nrevisions up through ch 3 exercises\n------------------------------------------------------------------------\nr2524 | bem | 2005-06-30 18:45:59 -0500 (Thu, 30 Jun 2005) | 1 line\n\nresults of read-through; including renaming mistyping to snapshots\n------------------------------------------------------------------------\n"))
  end

  def test_churn_line_to_int_extracts_parenthesized_change_count
    assert_equal(51,
                 churn_line_to_int(" (51 changes)   **********"))
    assert_equal(2,
                 churn_line_to_int(" (2 changes)   **********"))
  end

  def test_churn_line_to_int()
    array1 = ["(51 changes)","(9 changes)","(2 changes)"]
    array2 = ["(51 changes)","(2 changes)","(9 changes)"]
    assert_equal(array1, order_by_descending_change_count(array2))
  end

  def test_order_by_descending_change_count
    original = [ "all that really matters is the number in parens - (1 changes)",
                 "     inventory  (0 changes)",
                 "            ui ** (12 changes)" ]

    expected = [ "            ui ** (12 changes)",
                 "all that really matters is the number in parens - (1 changes)",
                 "     inventory  (0 changes)" ]

    actual = order_by_descending_change_count(original)

    assert_equal(expected, actual)
  end

  def test_interesting_lines_contain_at_least_one_asterisk

    boring_line =      "test           -    -"
    interesting_line = "keting.docx    (4 changes)    *"
    big_line =         "jiazhuang.xlsx (51 changes)   **********"

    original = [interesting_line, boring_line, big_line]
    expected = [interesting_line, big_line]

    assert_equal(expected, interesting(original))
  end

  def test_interesting_lines_subsystem_can_have_asterisk_at_end
    boring_line = subsystem_line('inventory*', 0)
    interesting_line = subsystem_line('ui*', 50)

    original = [interesting_line, boring_line]
    expected = [interesting_line]

    assert_equal(expected, interesting(original))
  end

  def test_rearrange_with_middle_name
    assert_equal("Dawn E. Marick", rearrange("Marick, Dawn Elaine"))
  end

  def test_rearrange_without_middle_name
    assert_equal("Paul Marick", rearrange("Marick, Paul"))
  end
end
